﻿<%@ WebHandler Language="C#" Class="admin" %>

using System;
using System.Web;
using System.Web.SessionState;
using System.Data;
using System.Data.SqlClient;

public class admin : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string cmd = context.Request.Form["cmd"];
        if (cmd == "stu")//学生信息
        {
            Int32 row = Convert.ToInt32(context.Request.Form["row"]);
            string sql = "select top 10 b.rownumber ,a.ID as id,a.姓名 as name,a.年级 as nianji,a.班级 as banji,a.xuehao,a.学院 as xueyuan,a.专业 as zhuanye,a.公寓楼 as gongyu,a.寝室号 as qinshi from [stu] as a LEFT JOIN (select row_number() over(order by id desc) as rownumber,* from [stu]) b  on a.id = b.id  where b.rownumber>"+row+" order by a.id desc";
            string tb =  ConvertJson.ToJson(SQLHelper.GetTable(sql));
            context.Response.Write(tb);
            return;
        }
        if (cmd == "stunoimg")//没有上传照片的学生
        {
            Int32 row = Convert.ToInt32(context.Request.Form["row"]);
            string sql ="select top 10 b.rownumber,a.ID as id,a.姓名 as name,a.年级 as nianji,a.班级 as banji,a.xuehao,a.学院 as xueyuan,a.专业 as zhuanye,a.公寓楼 as gongyu,a.寝室号 as qinshi from stu  as a LEFT JOIN (select row_number() over(order by id desc) as rownumber,* from [stu] where photo is null) b on a.id = b.id  where b.rownumber>"+row+" order by b.rownumber";
            string tb = ConvertJson.ToJson(SQLHelper.GetTable(sql));
            context.Response.Write(tb);
            return;
        }
        if (cmd == "stuphoto")//学生照片管理
        {
            Int32 row = Convert.ToInt32(context.Request.Form["row"]);
            string sql = "select top 70 b.rownumber,a.id,a.photo from stu as a LEFT JOIN (select row_number() over(order by id desc) as rownumber,* from [stu] where photo is not null) b on a.id = b.id  where b.rownumber>"+row+" and a.photo is not null order by b.rownumber";
            string tb = ConvertJson.ToJson(SQLHelper.GetTable(sql));
            context.Response.Write(tb);
            return;
        }
        if (cmd == "stuerror")//信息错误的学生
        {
            Int32 row = Convert.ToInt32(context.Request.Form["row"]);
            string sql = "select top 10 b.rownumber,a.ID as id,a.姓名 as name,a.年级 as nianji,a.班级 as banji,a.xuehao,a.学院 as xueyuan,a.专业 as zhuanye,a.公寓楼 as gongyu,a.寝室号 as qinshi from stu  as a LEFT JOIN (select row_number() over(order by id desc) as rownumber,* from [stu] where len(xuehao) <> 10) b on a.id = b.id  where b.rownumber>" + row + " order by b.rownumber";
            string tb = ConvertJson.ToJson(SQLHelper.GetTable(sql));
            context.Response.Write(tb);
            return;
        }
        if (cmd == "sousuo")//搜索
        {
            string sql,key = "";
            sql = "select ID as id,姓名 as name,年级 as nianji,班级 as banji,xuehao,学院 as xueyuan,专业 as zhuanye,公寓楼 as gongyu,寝室号 as qinshi from [stu] where ";
            string keyword = context.Request.Form["keyword"].ToString();
            if (IsChinaString(keyword))
            {
                key = "姓名='" + keyword + "'";
            }
            else
            {
                key = "ID=" + keyword + "or xuehao='" + keyword + "'";
            }
            sql = sql + key;
            string tb = ConvertJson.ToJson(SQLHelper.GetTable(sql));
            context.Response.Write(tb);
            return;
        }
        if (cmd == "xiangxi")//学生详细信息
        {
            Int32 id = Convert.ToInt32(context.Request.Form["id"]);
            string sql = "select ID as id,姓名 as name,年级 as nianji,班级 as banji,xuehao,学院 as xueyuan,专业 as zhuanye,公寓楼 as gongyu,寝室号 as qinshi,籍贯 as jiguan,资助与奖励 as zizhu,获奖情况 as huojiang,photo from [stu] where id=" + id;
            string tb = ConvertJson.ToJson(SQLHelper.GetTable(sql));
            context.Response.Write(tb);
            return;
        }
        if (cmd == "login")//教师登录
        {
            string uid = context.Request.Form["uid"].ToString();
            string pwd = context.Request.Form["pwd"].ToString();
            string sql = "select fid, 公寓楼 as gongyu,姓名 as name from fudaoyuan where 工号='"+uid+"' and 密码='"+pwd+"'";
            DataTable tb = SQLHelper.GetTable(sql);
            if (tb.Rows.Count > 0)
            {
                context.Response.Write(ConvertJson.ToJson(tb));
            }
            else
            {
                context.Response.Write("no");
            }
            return;
        }
        if (cmd == "fudao")//辅导员原始数据
        {
            string sql = "select a.fid,a.公寓楼,a.姓名,a.联系电话,a.短号,a.值班地点,a.工号,b.defen,b.timer from fudaoyuan as a  left join fdycj as b on a.fid=b.fid";
            string tb = ConvertJson.ToJson(SQLHelper.GetTable(sql));
            context.Response.Write(tb);
            context.Response.End();
        }
        if (cmd == "gaodi")//从高到低
        {
            string sql = "select a.fid,a.公寓楼,a.姓名,a.联系电话,a.短号,a.值班地点,a.工号,b.defen,b.timer from fudaoyuan as a  left join fdycj as b on a.fid=b.fid order by b.defen desc";
            string tb = ConvertJson.ToJson(SQLHelper.GetTable(sql));
            context.Response.Write(tb);
            context.Response.End();
        }
        if (cmd == "digao")//从低到高
        {
            string sql = "select a.fid,a.公寓楼,a.姓名,a.联系电话,a.短号,a.值班地点,a.工号,b.defen,b.timer from fudaoyuan as a  left join fdycj as b on a.fid=b.fid order by b.defen";
            string tb = ConvertJson.ToJson(SQLHelper.GetTable(sql));
            context.Response.Write(tb);
            context.Response.End();
        }
        if (cmd == "clearrank")//清空得分
        {
            if (SQLHelper.ExcuteSQL("update fdycj set defen=null") > 0)
            {
                string sql = "select a.fid,a.公寓楼,a.姓名,a.联系电话,a.短号,a.值班地点,a.工号,b.defen,b.timer from fudaoyuan as a  left join fdycj as b on a.fid=b.fid";
                string tb = ConvertJson.ToJson(SQLHelper.GetTable(sql));
                context.Response.Write(tb);
                context.Response.End();
            }
        }
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
    public bool IsChinaString(string CString)
    {
        bool BoolValue = false;
        for (int i = 0; i < CString.Length; i++)
        {
            if (Convert.ToInt32(Convert.ToChar(CString.Substring(i, 1))) < Convert.ToInt32(Convert.ToChar(128)))
            {
                BoolValue = false;
            }
            else
            {

                return BoolValue = true;
            }
        }

        return BoolValue;

    }

}